package com.ml.loan.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.loan.common.model.PageResult;
import com.ml.loan.common.utils.Convert;
import com.ml.loan.sys.dao.SysOperRecordMapper;
import com.ml.loan.sys.model.SysOperRecord;
import com.ml.loan.sys.service.SysOperRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 操作日志 服务实现类
 * </p>
 *
 * @author milian
 * @since 2022-08-22
 */
@Service
public class SysOperRecordServiceImpl extends ServiceImpl<SysOperRecordMapper, SysOperRecord> implements SysOperRecordService {

    @Async
    @Override
    public void saveAsync(SysOperRecord operRecord) {
        save(operRecord);
    }

    @Override
    public PageResult<SysOperRecord> list(Integer page, Integer limit, String startDate, String endDate, String searchKey, String searchValue, Integer userType) {
        QueryWrapper<SysOperRecord> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(searchValue) && StringUtils.isNotBlank(searchKey)) {
            wrapper.likeRight(searchKey, searchValue);
        }
        if (StringUtils.isNotBlank(startDate)) {
            wrapper.ge("r.create_time", startDate + " 00:00:00");
        }
        if (StringUtils.isNotBlank(endDate)) {
            wrapper.le("r.create_time", endDate + " 23:59:59");
        }
        if (userType != null) {
            wrapper.eq("r.user_type", userType);
        }
        wrapper.orderByDesc("r.create_time");
        Page<SysOperRecord> pageinfo = new Page<>(Convert.toInt(page, 1), Convert.toInt(limit, 10));
        List<SysOperRecord> data = baseMapper.list(pageinfo, wrapper);
        return new PageResult<>(data, pageinfo.getTotal());
    }
}
